define( [
	'jquery',
	'require',
	'header',
	'message',
	'api',
	'dateRange',
	'selector',
	'tooltip',
	'bigwe_utils',
	'bigwe_component',
	'pagination',
	'artTemplate',
	'nav'
], function( $, require ) {

	var bu = require( 'bigwe_utils' );
	var bc = require( 'bigwe_component' );
	//var thisApi = require( 'api' ).GATHER;
	var pickerDateRange = require( 'dateRange' );
	var at = require( 'artTemplate' );

	at.helper( 'escape', function( data ) {
		return escape( data );
	} );

	var k_Milliseconds = Date.now() - 86400000 * 30,
		jieshu_date = new Date().format( 'yyyy-MM-dd' ),
		kaishi_date = new Date( k_Milliseconds ).format( 'yyyy-MM-dd' );

	var selectStr = '.mygather_lower .item td input[type="checkbox"]';


	$( '.selectpicker' ).selectpicker( {
		size: 8
	} );


	var classArray = {
		'1': {
			className: 'audit_through',
			text: '审核通过'
		},
		'-1': {
			className: 'audit_failure',
			text: '审核未通过'
		},
		'0': {
			className: 'pending_audit',
			text: '待审核'
		}
	};
	var myGather = {
		searchParam: {
			st: kaishi_date,
			et: jieshu_date,
			p: ($('#page').val() || 1),
			l: 10
		},
		doSearch: function( searchParam, isreloadPage ) {
			var pageParam = {};

			if ( typeof searchParam === 'string' ) {
				pageParam.currentPage = $( '#pagination .current_page' ).data( 'currentPage' );
				searchParam = {};
			}

			$( '.mygather_lower table.item' ).remove();
			//$('#pagination').hide();
			return $.post( '/hot_topic/get_account_list', $.extend( this.searchParam, searchParam ), function( data ) {

				data = typeof data === 'string' ? $.parseJSON( data ) : data;
				var gatherItem = null;
				if ( data.ret === 0 ) {
					for ( var i in data.data.account ) {
						var dataItem = data.data.account[ i ];
						gatherItem = $( at( 'gather_item', dataItem ) );
						$( '.item_platform', gatherItem.addClass( ( classArray[ dataItem.status ] && classArray[ dataItem.status ].className ) || '' ) ).text( $( '.platform [value="' + dataItem.platform + '"]' ).text() );
						$( '.item_state', gatherItem ).text( ( classArray[ dataItem.status ] && classArray[ dataItem.status ].text ) || '' );

						$( '[data-toggle="tooltip"]', gatherItem ).tooltip();
						$( '.mygather_lower' ).append( gatherItem );
					}

					$( '#pageCount' ).text( data.data.count || 0 )
					if ( isreloadPage && data.data.count > this.searchParam.l ) {
						this.paging( $.extend( {
							total: data.data.count,
                            currentPage: this.searchParam.p
						}, pageParam ) ).show();
					} else {
						isreloadPage && this.paging( {
							total: 0
						} ).hide();
					};

					if ( !this.accountFilterLoad ) {
						var options = at.compile( '{{each list}}<option value="{{$value}}">{{$value}}</option>{{/each}}' )( {
							list: data.data.target
						} );
						$( '.mygather_lower select.account' ).append( options ).selectpicker( 'refresh' );
						this.accountFilterLoad = true;
					}

				}

			}.bind( this ) );
		},
		paging: function( total ) {
			return $( "#pagination" ).bwPagination( $.extend( {
				callback: function( _index ) {
					$( 'body' ).animate( {
						scrollTop: $( '.main .main_left' ).offset().top
					}, .2e3 );
					myGather.doSearch( {
						p: _index
					} );

                    history.replaceState( null, document.title, '/HotTopic/account_verify?p=' + _index );
				}.bind( this )
			}, total ) );
		}
	}

	myGather.doSearch( null, true );

	var utils = {
		deleteBack: function() {
			$( this ).remove();
			utils.changeCheckStatus();
			myGather.doSearch( 'refresh' );
		},
		changeCheckStatus: function() {
			var checkboxLen = $( selectStr ).length;
			var checkedLen = $( selectStr + ':checked' ).length;
			var $checkall = $( '#checkall' );
			if ( checkedLen && checkboxLen !== checkedLen ) {
				$checkall[ 0 ].indeterminate = true;
				$( '#deleteItem' ).removeAttr( 'disabled' );
			} else if ( checkboxLen === checkedLen ) {
				$checkall.prop( 'checked', 'checked' );
				$checkall[ 0 ].indeterminate = false;
				$( '#deleteItem' ).removeAttr( 'disabled' );
			} else {
				$checkall[ 0 ].indeterminate = false;
				$( '#deleteItem' ).prop( 'disabled', 'disabled' );
			}
			$( '.mygather_lower .title .number' ).text( checkedLen ? ( checkedLen + '条' ) : '无' );
		}
	};

	// 单个删除
	$( document ).on( 'click', '.mygather_lower .item .js_agree', function() {
		bc.deleteConfirmShowBottom( {
			obj: this,
			url: '/hot_topic/agree_verify',
			type: 'post',
			param: {
				ids: $( this ).data( 'id' )
			},
			fun: function( data ) {
				/*data = $.parseJSON( data );
				if ( data.ret === 0 ) {
					utils.deleteBack.call( $( this ).parents( 'table.item' ) );

				}*/
			}.bind( this )

		} );
	} ).on( 'change', 'select.platform', function() { // 按平台查找
		myGather.doSearch( {
			p: 1,
			platform: $( this ).val()
		}, true );
	} ).on( 'change', 'select.account', function() { // 按账号
		myGather.doSearch( {
			p: 1,
			name: $( this ).val()
		}, true );
	} ).on( 'change', 'select.state', function() { // 按状态
		myGather.doSearch( {
			p: 1,
			status: $( this ).val()
		}, true );
	} ).on( 'click', '.js_dosearch', function() { // 搜索
		myGather.doSearch( {
			p: 1,
			key: $( '#js_ipt_dosearch' ).val()
		}, true );
	} ).on( 'reset', '#frm_search', function() { // 重置
		setTimeout( function() {
			$( '.selectpicker' ).selectpicker( 'refresh' );
		} );
		$( '#date-fromto-text' ).text( kaishi_date + ' 至 ' + jieshu_date );

		myGather.doSearch( {
			p: 1,
			key: '',
			st: kaishi_date,
			et: jieshu_date,
			status: '',
			name: '',
			platform: ''
		}, true );
	} ).on( 'change', selectStr, function() {
		utils.changeCheckStatus();
	} ).on( 'change', '#checkall', function() {
		if ( this.checked ) {
			$( selectStr ).prop( 'checked', 'checked' ).trigger( 'change' );
		} else {
			$( selectStr ).removeAttr( 'checked' ).trigger( 'change' );
		}
	} ).on( 'click', '#deleteItem', function() { // 选中删除
		var deleteIds = $( selectStr + ':checked' ).map( function() {
			return $( this ).parents( 'table.item' ).data( 'id' );
		} ).get();

		/*bc.deleteConfirmShowBottom( {
			obj: this,
			text: '确定要通过这<span class="green">' + deleteIds.length + '</span>审核吗？',
			url: '/hot_topic/agree_verify',
			type: 'post',
			param: {
				ids: deleteIds.join( ',' )
			},
			fun: function( data ) {

				data = $.parseJSON( data );
				if ( data.ret === 0 ) {
					utils.deleteBack.call( $( selectStr + ':checked' ).parents( 'table.item' ) );

				}

				$( this ).prop( 'disabled', 'disabled' );
			}.bind( this )
		} );*/
	} );

	new pickerDateRange( 'date-fromto-text', {
		isTodayValid: true,
		startDate: kaishi_date,
		endDate: jieshu_date,
		isSingleDay: false, // 是否单选天
		shortOpr: false, // 是否只选一天
		defaultText: ' 至 ',
		inputTrigger: 'date-fromto',
		theme: 'ta',
		stopToday: true,
		success: function( obj ) {
			myGather.doSearch( {
				p: 1,
				st: obj.startDate,
				et: obj.endDate
			}, true );
		}
	} );




} );
